﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PictureTool.Model;
using System.Drawing.Imaging;
using System.IO;

namespace PictureTool
{
    public partial class ImgCut : Form
    {
        private List<RectangleModel> RModelList = new List<RectangleModel>();
        private int y = 0;//y初始值
        private int yAfter = 0;//点击之后
        private string imgName = string.Empty;
        private string imgPath = string.Empty;
        private int width = 0;
        public ImgCut()
        {
            InitializeComponent();
        }

        private void pictureBoxMain_MouseMove(object sender, MouseEventArgs e)
        {
            this.tBoxDqzb.Text = e.X + "," + e.Y;
        }

        /// <summary>
        /// 存入集合
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCrjh_Click(object sender, EventArgs e)
        {
            try
            {
                string[] dqjx = this.tBoxDqjx.Text.Split(',');
                RectangleModel rm = new RectangleModel() { X = Convert.ToInt32(dqjx[0]), Y = Convert.ToInt32(dqjx[1]), W = Convert.ToInt32(dqjx[2]), H = Convert.ToInt32(dqjx[3]) };
                RModelList.Add(rm);

                y = yAfter;
            }
            catch (Exception ex)
            {
                MessageBox.Show("增加失败!"+ex.Message);
                return;
            }
            MessageBox.Show("增加成功已经有了" + RModelList.Count);


        }
        /// <summary>
        /// 切割图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQgtp_Click(object sender, EventArgs e)
        {
            try
            {
                if (RModelList.Count > 0)
                {
                    Bitmap bmpBase = new Bitmap(imgPath);//构造图片对象
                    string newFilePath = string.Empty;
                    Bitmap bmpNew = null;
                    int i = 0;
                    foreach (RectangleModel rm in RModelList)
                    {
                        Rectangle rect = new Rectangle(rm.X,rm.Y,rm.W,rm.H);
                        bmpNew = bmpBase.Clone(rect, bmpBase.PixelFormat);//将这个区域克隆成新的图片
                        newFilePath = string.Format("{0}\\{1}", Config.OutPutPath, i++ + ".jpg");
                        bmpNew.Save(newFilePath, ImageFormat.Jpeg);//保存
                    }

                    bmpBase.Dispose();// 释放资源
                    bmpNew.Dispose();//释放资源
                }
                else
                {
                    MessageBox.Show("集合中无数据");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("切割失败!" + ex.Message);
            }

            MessageBox.Show("切割成功");
        }

      

        private void pictureBoxMain_MouseDown(object sender, MouseEventArgs e)
        {
            tBoxDqjx.Text = String.Format("{0},{1},{2},{3}",0,y,width,e.Y-y);
            yAfter = e.Y;
        }

        private void ImgCut_Load(object sender, EventArgs e)
        {
            string[] files = System.IO.Directory.GetFiles(Config.PicturePath);
             imgName =  Path.GetFileName(files[0]);
             imgPath = string.Format("{0}\\{1}", Config.PicturePath, imgName);
            this.pictureBoxMain.Image = Image.FromFile(imgPath);
            width = this.pictureBoxMain.Image.Width;//获得图片宽度
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            //this.VerticalScroll.Value = yAfter+400;
            //MessageBox.Show(this.VerticalScroll.Maximum);
        }
    }
}
